//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "common\fn_storeObject.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;
private ["_object","_instance_id","_instance_content","_instances","_mission_objects","_jip_instance","_jip_objects"];
DEFAULT_PARAM(_object,0,objNull);
if (isnil QGVAR(STORING_OBJECT_BUSY)) then { GVAR(STORING_OBJECT_BUSY) = -1; };
waituntil {(GVAR(STORING_OBJECT_BUSY) < time)};
GVAR(STORING_OBJECT_BUSY) = time + 10;
_instance_id = [_object] call FFUNC(GetInstanceID);
_instance_content = OGETVAR(GVAR(mission_store),_instance_id,(call FFUNC(hashEmpty)));
_mission_objects = [_instance_content, TMF_HK_MISSION_OBJECTS,[],THIS_FILE] call FFUNC(hashGet);
if (_object in _mission_objects) then {
	TRACE_1("Object already stored",_object);
} else {
	TRACE_1("Adding object to the store",ARR_3(_object,_instance_id,count _mission_objects));
	_mission_objects set [ count _mission_objects, _object];
	_instance_content = [_instance_content, TMF_HK_MISSION_OBJECTS,_mission_objects] call FFUNC(HashSet);
	OSETVAR(GVAR(mission_store),_instance_id,_instance_content);
};
_instances = OGETVAR(GVAR(mission_store),QGVAR(MISSION_INSTANCES),[]);
if (!(_instance_id in _instances)) then {
	_instances set [count _instances, _instance_id];
	OSETVAR(GVAR(mission_store),QGVAR(MISSION_INSTANCES),_instances);
};
if (isServer) then {
	_instances = JIPGETVAR(QGVAR(MISSION_INSTANCES),[]);
	if (!(_instance_id in _instances)) then {
		_instances set [count _instances, _instance_id];
		JIPSETVAR(QGVAR(MISSION_INSTANCES),_instances);
		TRACE_1("Instances",_instances);
	};
	_jip_instance = [_instance_id] call FFUNC(getMissionInstance);
	_jip_objects = [_jip_instance, TMF_HK_MISSION_OBJECTS,[],THIS_FILE] call FFUNC(hashGet);
	if (_object in _jip_objects) then {
		TRACE_1("Object already jipped",_object);	
	} else {
		TRACE_1("Adding object to the JIP store",ARR_3(_object,_instance_id,count _jip_objects));
		_jip_objects set [ count _jip_objects, _object];
		_jip_instance = [_jip_instance, TMF_HK_MISSION_OBJECTS,_jip_objects] call FFUNC(HashSet);
		[_instance_id,_jip_instance] call FFUNC(setMissionInstance);
	};
};
GVAR(STORING_OBJECT_BUSY) = -1;
TRACE_1("End",ARR_2(_instance_id, count _mission_objects));
TMFPROFILERSTOP;
